#include "include/paramnonstandard485.h"
#include <QDebug>
#include <QSettings>
#include <QTextCodec>

ParamNonStandard485::ParamNonStandard485() {}

ParamNonStandard485::~ParamNonStandard485() {}

void ParamNonStandard485::readParam( const QString& config_name ) {
    //    qDebug() << "readParam path" << config_name + "NonStandard_485.ini";
    QSettings nonstandard_param( config_name + "NonStandard_485.ini", QSettings::IniFormat );
    nonstandard_param.setIniCodec( QTextCodec::codecForName( "UTF-8" ) );
    nonstandard_param.beginGroup( "nonstandard_485_init" );
    /*********串口**************/
    qDebug() << "SerialPortName:" << nonstandard_param.value( "SerialPortNameParameter", "comtys1" ).toString();
    serialConfig.SerialPortNameParameter = nonstandard_param.value( "SerialPortNameParameter", "comtys1" ).toString();
    serialConfig.SerialParityParameter   = nonstandard_param.value( "SerialParityParameter", 0 ).toInt();
    serialConfig.SerialBaudRateParameter = nonstandard_param.value( "SerialBaudRateParameter", 9600 ).toInt();
    serialConfig.SerialDataBitsParameter = nonstandard_param.value( "SerialDataBitsParameter", 8 ).toInt();
    serialConfig.SerialStopBitsParameter = nonstandard_param.value( "SerialStopBitsParameter", 1 ).toInt();
    serialConfig.SerialTimeout           = nonstandard_param.value( "SerialTimeout", 3000 ).toInt();
    serialConfig.SerialFrequency         = nonstandard_param.value( "SerialFrequency", 100 ).toInt();
    /*********串口**************/
    /*********传输**************/
    transmissionConfig.Car_OPEN_Transmission = nonstandard_param.value( "Car_OPEN_Transmission", 0 ).toInt();
    if ( transmissionConfig.Car_OPEN_Transmission == 1 ) {
        transmissionConfig.Car_UP_Left_StatID        = nonstandard_param.value( "Car_UP_Left_StatID", 1 ).toInt();
        transmissionConfig.Car_UP_Right_StatID       = nonstandard_param.value( "Car_UP_Right_StatID", 2 ).toInt();
        transmissionConfig.Car_Down_Left_StatID      = nonstandard_param.value( "Car_Down_Left_StatID", 3 ).toInt();
        transmissionConfig.Car_Down_Right_StatID     = nonstandard_param.value( "Car_Down_Right_StatID", 4 ).toInt();
        transmissionConfig.TransmissionReadFrequency = nonstandard_param.value( "TransmissionReadFrequency", 3000 ).toInt();
        transmissionConfig.TransmissionSpeed         = nonstandard_param.value( "TransmissionSpeed", 3000 ).toInt();
        transmissionConfig.TransmissionWaiterTimer   = nonstandard_param.value( "TransmissionWaiterTimer", 100 ).toInt();
    }
    /*********传输**************/
    /*********电池**************/
    batterConfig.Car_OPEN_Batter = nonstandard_param.value( "Car_OPEN_Batter", 0 ).toInt();
    if ( batterConfig.Car_OPEN_Batter == 1 ) {
        batterConfig.Car_Battery_StartID  = nonstandard_param.value( "Car_Battery_StartID", 1 ).toInt();
        batterConfig.BatteryReadFrequency = nonstandard_param.value( "BatteryReadFrequency", 1000 ).toInt();
        batterConfig.BatteryWaitertimer   = nonstandard_param.value( "BatteryWaitertimer", 100 ).toInt();
    }
    /*********电池**************/
    /*********卷帘门*****************/
    shurtDoorConfig.Car_OPEN_ShutterDoor = nonstandard_param.value( "Car_OPEN_ShutterDoor", 0 ).toInt();
    if ( shurtDoorConfig.Car_OPEN_ShutterDoor ) {
        shurtDoorConfig.Car_ShutterDoor_StatID   = nonstandard_param.value( "Car_ShutterDoor_StatID", 1 ).toInt();
        shurtDoorConfig.ShutterDoorReadFrequency = nonstandard_param.value( "ShutterDoorReadFrequency", 100 ).toInt();
        shurtDoorConfig.ShutterDoorDownSpeed     = nonstandard_param.value( "ShutterDoorDownSpeed", 1000 ).toInt();
        shurtDoorConfig.ShutterDoorUpSpeed       = nonstandard_param.value( "ShutterDoorUpSpeed", 1000 ).toInt();
        shurtDoorConfig.ShutterDoorWaiterTimer   = nonstandard_param.value( "ShutterDoorWaiterTimer", 100 ).toInt();
    }
    nonstandard_param.endGroup();
}

void ParamNonStandard485::printParam() {
    qDebug() << "串口****";
    qDebug() << "SerialPortNameParameter: " << serialConfig.SerialPortNameParameter;
    qDebug() << "SerialParityParameter: " << serialConfig.SerialParityParameter;
    qDebug() << "SerialBaudRateParameter: " << serialConfig.SerialBaudRateParameter;
    qDebug() << "SerialDataBitsParameter: " << serialConfig.SerialDataBitsParameter;
    qDebug() << "SerialStopBitsParameter: " << serialConfig.SerialStopBitsParameter;
    qDebug() << "SerialTimeout: " << serialConfig.SerialTimeout;
    qDebug() << "SerialFrequency: " << serialConfig.SerialFrequency;
    qDebug() << "传输****";
    qDebug() << "Car_OPEN_Transmission: " << transmissionConfig.Car_OPEN_Transmission;
    qDebug() << "Car_UP_Left_StatID: " << transmissionConfig.Car_UP_Left_StatID;
    qDebug() << "Car_UP_Right_StatID: " << transmissionConfig.Car_UP_Right_StatID;
    qDebug() << "Car_Down_Left_StatID: " << transmissionConfig.Car_Down_Left_StatID;
    qDebug() << "Car_Down_Right_StatID: " << transmissionConfig.Car_Down_Right_StatID;
    qDebug() << "TransmissionReadFrequency: " << transmissionConfig.TransmissionReadFrequency;
    qDebug() << "TransmissionSpeed: " << transmissionConfig.TransmissionSpeed;
    qDebug() << "TransmissionWaiterTimer: " << transmissionConfig.TransmissionWaiterTimer;
    qDebug() << "卷帘门***";
    qDebug() << "Car_OPEN_ShutterDoor: " << shurtDoorConfig.Car_OPEN_ShutterDoor;
    qDebug() << "Car_ShutterDoor_StatID: " << shurtDoorConfig.Car_ShutterDoor_StatID;
    qDebug() << "ShutterDoorReadFrequency: " << shurtDoorConfig.ShutterDoorReadFrequency;
    qDebug() << "ShutterDoorUpSpeed: " << shurtDoorConfig.ShutterDoorUpSpeed;
    qDebug() << "ShutterDoorDownSpeed: " << shurtDoorConfig.ShutterDoorDownSpeed;
    qDebug() << "ShutterDoorWaiterTimer: " << shurtDoorConfig.ShutterDoorWaiterTimer;
    qDebug() << "电池*****";
    qDebug() << "Car_OPEN_Batter: " << batterConfig.Car_OPEN_Batter;
    qDebug() << "Car_Battery_StartID: " << batterConfig.Car_Battery_StartID;
    qDebug() << "BatteryReadFrequency: " << batterConfig.BatteryReadFrequency;
    qDebug() << "BatteryWaitertimer: " << batterConfig.BatteryWaitertimer;
}

ParamNonStandard485::SerialConfig* ParamNonStandard485::getSerialConfig() {
    return &serialConfig;
}

ParamNonStandard485::TransmissionConfig* ParamNonStandard485::getTransmissionConfig() {
    return &transmissionConfig;
}

ParamNonStandard485::ShurtDoorConfig* ParamNonStandard485::getShurtDoorConfig() {
    return &shurtDoorConfig;
}

ParamNonStandard485::BatterConfig* ParamNonStandard485::getBatterConfig() {
    return &batterConfig;
}
